package com.hardroid.workload.ui.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hardroid.workload.R;
import com.hardroid.workload.utils.Singleton;

public class EditProjectFragment extends Fragment {
	
	private EditText observerName;
	private EditText companyName;
	private EditText companyLocation;

	private Button cancelButton;
	private Button saveButton;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_edit_project, container,false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		observerName = (EditText) getView().findViewById(R.id.edit_project_observer_name_text);
		companyName = (EditText) getView().findViewById(R.id.edit_project_company_name_text);
		companyLocation = (EditText) getView().findViewById(R.id.edit_project_company_location_text);

		cancelButton = (Button) getView().findViewById(R.id.edit_project_cancel_button);
		saveButton = (Button) getView().findViewById(R.id.edit_project_save_button);

		cancelButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getActivity().finish();
			}
		});

		saveButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {				
				if (Singleton.currentProject!=null){				
					if (!companyName.getText().toString().equals("")&&!observerName.getText().toString().equals("")&&!companyLocation.getText().toString().equals("")){
						Singleton.currentProject.companyLocation= companyLocation.getText().toString();
						Singleton.currentProject.observerName = observerName.getText().toString();
						Singleton.currentProject.companyName = companyName.getText().toString();
						
						Singleton.saveProject(Singleton.currentProject);
						getActivity().finish();
					}else{
						Toast.makeText(getActivity(),"Changes are not valid",Toast.LENGTH_SHORT).show();
					}					
				}else{
					Toast.makeText(getActivity(), "Project Error",Toast.LENGTH_SHORT).show();
				}
				
			}
		});
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		if (Singleton.currentProject != null) {			
			observerName.setText(Singleton.currentProject.observerName);
			companyName.setText(Singleton.currentProject.companyName);
			companyLocation.setText(Singleton.currentProject.companyLocation);		
		}	
	}
}
